﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace VisionCloud.Domain.Filters
{
    /// <summary>
    /// 限制枚举选项
    /// </summary>
    public class LimitedEnumAttribute : ValidationAttribute
    {
        private readonly Enum[] _allowedValues;

        public LimitedEnumAttribute(Type enumType, params int[] allowedValues)
        {
            _allowedValues = allowedValues
                .Select(value => (Enum)Enum.ToObject(enumType, value))
                .ToArray();
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (_allowedValues == null || _allowedValues.Length == 0)
            {
                return ValidationResult.Success;
            }

            if (value is Enum enumValue && _allowedValues.Contains(enumValue))
            {
                return ValidationResult.Success;
            }

            string allowedValuesString = string.Join(", ", _allowedValues.Select(e => e.ToString()));
            return new ValidationResult($"Invalid value for {validationContext.MemberName}. Allowed values are: {allowedValuesString}");
        }
    }
}
